package com.deppon.vts.funny;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.deppon.vts.funny.bean.FunnyLogicRequestEntity;
import com.deppon.vts.funny.bean.FunnyLogicResponseEntity;
import com.deppon.vts.micro.exception.BizException;
import com.deppon.vts.micro.service.biz.annotation.Logic;
import com.deppon.vts.micro.service.biz.bean.BizContext;
import com.deppon.vts.micro.service.logic.LogicQueryService;

@Logic(transactional=true)
public class FunnyLogicService extends LogicQueryService<FunnyLogicRequestEntity, FunnyLogicResponseEntity> {
	
	private int width = 200;
	private int height = 300;
	
	@Override
	public void processRequest(BizContext<FunnyLogicRequestEntity, FunnyLogicResponseEntity> context)
			throws BizException {
		sendImage(context, drawImage(context.getRequest().getText()));
	}
	
	private BufferedImage drawImage(String text){
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setColor(Color.WHITE);
        Font font = new Font("微软雅黑",4,20);
        g2.setFont(font);
        g2.drawString(text, 0, 0);
        g2.dispose();
        return image;
	}
	
	public static void main(String[] args) {
		System.out.println(1);
		System.out.println(2);
		new FunnyLogicService().drawImage("abc");
		System.out.println(Thread.activeCount());
		
		Map<Thread, StackTraceElement[]> allStacks = Thread.getAllStackTraces();
	    Map<Thread.State, Map<Thread, StackTraceElement[]>> classifyThreads = new HashMap<Thread.State, Map<Thread, StackTraceElement[]>>();
	    if (allStacks != null) {
	        Iterator<Map.Entry<Thread, StackTraceElement[]>> stackIterator = allStacks.entrySet().iterator();
	        while (stackIterator.hasNext()) {
	            Map.Entry<Thread, StackTraceElement[]> stackEntry = stackIterator.next();
	            Thread thread = stackEntry.getKey();
	            Thread.State state = thread.getState();
	            StackTraceElement[] traceElements = stackEntry.getValue();
	            Map<Thread, StackTraceElement[]> threadStacks = classifyThreads.get(state);
	            if(threadStacks == null) {
	                threadStacks = new HashMap<Thread, StackTraceElement[]>();
	                classifyThreads.put(state, threadStacks);
	            }
	            threadStacks.put(thread, traceElements);

	        }
	    }

	    for(Thread.State state : Thread.State.values()) {
	        Map<Thread, StackTraceElement[]> threadStacks = classifyThreads.get(state);
	        if(threadStacks != null) {
	            Iterator<Map.Entry<Thread, StackTraceElement[]>> stackIterator = threadStacks.entrySet().iterator();
	            System.out.println("<b>"+state.name()+"("+threadStacks.size()+")</b><br/><hr/>");
	            while (stackIterator.hasNext()) {
	                Map.Entry<Thread, StackTraceElement[]> stackEntry = stackIterator.next();
	                Thread thread = stackEntry.getKey();
	                StackTraceElement[] traceElements = stackEntry.getValue();
	                System.out.println("<pre>");
	                System.out.println("<a href=\"javascript:void(0);\" onclick=\"return toggleStacks('thread-"+thread.getId()+"');\">" + thread + "-" + thread.getState() + ":" + thread.getId()  + "(" + traceElements.length + ")</a>");
	                if(traceElements != null) {
	                	System.out.println("<div id=\"thread-"+thread.getId()+"\" style=\"display:none;\">");
	                    for(StackTraceElement stack : traceElements) {
	                    	System.out.println("    " + stack + " - " + stack.getFileName());
	                    }
	                    System.out.println("<div>");
	                }
	                System.out.println("</pre>");
	            }
	            System.out.println("<br/>");
	        }
	    }
	    System.out.println(Runtime.getRuntime().freeMemory()/(1024*1024)+"M");
	    System.out.println(Runtime.getRuntime().totalMemory()/(1024*1024)+"M");
	    System.out.println(Runtime.getRuntime().maxMemory()/(1024*1024)+"M");
	}

}
